﻿using System;
using System.Collections;
using System.Data;
using Service;
using Resources;
using  Model;


public partial class EquipmentAdd2 : System.Web.UI.Page
{
    readonly  StockService  _StockService  = new StockService();
    private int purchaseId;
    private int purchasedQuantity;
    private DataTable _ListSuggestedCodeNumber;
    private int modelId;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        purchaseId = !String.IsNullOrEmpty(Request.QueryString["purchaseId"])
                          ? Int32.Parse(Request.QueryString["purchaseId"]) : 0;
        purchasedQuantity = !String.IsNullOrEmpty(Request.QueryString["quantity"])
                    ? Int32.Parse(Request.QueryString["quantity"]) : 0;
        modelId = !String.IsNullOrEmpty(Request.QueryString["modelId"])
                   ? Int32.Parse(Request.QueryString["modelId"]) : Int32.Parse(Resource.EquipModelDefaultId);

        
        if(!IsPostBack)
        {
          
            if(!String.IsNullOrEmpty(Request.QueryString["purchaseId"]))
            {
                chkSetAutoCode.Checked = true;
                GenerateAvailableCodeNumbers(purchasedQuantity);
                InitializeData(purchasedQuantity);
            }
           
            
        }
        
    }
 
    public string GetAttachedParameters()
    {
        string param = "quantity=" + Request.QueryString["quantity"] +
                       "&typeId=" + Request.QueryString["typeId"] +
                       "&companyId=" + Request.QueryString["companyId"] +
                       "&purchaseDate=" + Request.QueryString["purchaseDate"] +
                       "&equipmentId=" + Request.QueryString["equipmentId"]+
                       "&poNumber=" + Request.QueryString["poNumber"];

                        
        return param;
    }
    private void InitializeData(int purchaseQuantity)
    {
        lblQuantity.Text = "There are " + purchaseQuantity + "  item(s) in this purchase item";
        lblPONumber.Text = Request.QueryString["poNumber"];

    }

    public void GenerateAvailableCodeNumbers(int quantity)
    {
        DataTable _ListSuggestedCodeNumber = new DirectService().GetSuggestedCodeNumbers(quantity);
        if (_ListSuggestedCodeNumber.Rows.Count == 0)
        {
            int maxNUM = _StockService.GetMaxEquipmentCodeNumber();
            IList codeNumberList = new UitilityService().GetRangeCodeNumber(maxNUM, purchasedQuantity);
            drlAutoCode.DataSource = codeNumberList;
        }
        else
        {
            drlAutoCode.DataSource = _ListSuggestedCodeNumber;
            drlAutoCode.DataTextField = "SuggestedNumber";
            drlAutoCode.DataValueField = "SuggestedNumber";
            drlAutoCode.DataBind();
        }
    }
   
    protected void btnValidate_Click1(object sender, EventArgs e)
    {
        // insert base on the number of purchase order
        purchasedQuantity = Request.QueryString["quantity"] != null
                        ? Int32.Parse(Request.QueryString["quantity"]) : 0;
       if(purchasedQuantity != 0 )
       {
         
           if (chkSetAutoCode.Checked)
           {
               //Request IList of code number  = purchase quantity
               

               IList codeNumberList = GetSuggestedCodeNumber();
               for (int i = 0; i < purchasedQuantity; i++)
               {
                   if (Request.QueryString["sparepart"] == null)
                   {
                       Equipment equipment = new Equipment();
                       equipment.EquipmentId = 0;
                       equipment.ModelId = Request.QueryString["modelId"] != null
                                               ? Int32.Parse(Request.QueryString["modelId"])
                                               : 0;
                       equipment.PurchaseId = Request.QueryString["purchaseId"] != null
                                                  ? Int32.Parse(Request.QueryString["purchaseId"])
                                                  : 0;
                       equipment.EstimatedEOL = DateTime.Parse(Resource.DateTimeNull);
                       equipment.CodeNumber = codeNumberList[i].ToString();
                       _StockService.Save(equipment);
                       // owner , unuse = 1
                       EquipmentOwner owner = new EquipmentOwner();
                       owner.OwnerId = 0;
                       owner.UnUse = false;
                       owner.CodeNumber = equipment.CodeNumber;

                       //set default user  = stockuser whose id  = 186
                       owner.EmployeeId = Int32.Parse(Resource.StockUserDefaultId);
                       owner.DateEnter = DateTime.Parse(Resource.DateTimeNull);
                       owner.SystemId = Int32.Parse(Resource.EquipSystemDefaultId);
                       owner.TypeId = Int32.Parse(Resource.EquipSystemTypeDefaultId);

                       _StockService.InsertEquipmentOwner(owner);
                   }
                  

               }
               btnValidate.Enabled = false;
           }
           else
           {
               Equipment equipment = new Equipment();
               equipment.EquipmentId = 0;
               equipment.ModelId = Request.QueryString["modelId"] != null
                           ? Int32.Parse(Request.QueryString["modelId"]) : 0;
               equipment.PurchaseId = Request.QueryString["purchaseId"] != null
                               ? Int32.Parse(Request.QueryString["purchaseId"]) : 0;
               equipment.EstimatedEOL = DateTime.Parse(Resource.DateTimeNull);
               equipment.CodeNumber = txtCode.Text;
               _StockService.Save(equipment);
               btnValidate.Enabled = true;
           }
       }
    }
    private IList GetSuggestedCodeNumber()
    {
        IList listCodeNumbers = new ArrayList();
        for (int i = 0; i < drlAutoCode.Items.Count;i++ )
        {
            listCodeNumbers.Add(drlAutoCode.Items[i].Text);
          
        } 
        return listCodeNumbers;
    }
    protected void drlPONumber_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnValidate.Enabled = true;
    }
    protected void chkSetAutoCode_CheckedChanged(object sender, EventArgs e)
    {
        
    }
    public string ConfirmMessage  ()
    {
        return "Are you see to delete this employee?";
    }
}
